package com.novel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.novel.mapper.HistoryTagsMapper;
import com.novel.mapper.NovelMapper;
import com.novel.mapper.ScoreMapper;
import com.novel.service.NovelService;
import com.reading.pojo.*;
import com.reading.pojo.user.User;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service
public class NovelSeriveImpl implements NovelService {

    @Resource
    private NovelMapper novelMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private HistoryTagsMapper historyTagsMapper;
    @Override
    public List<Novel> list() {
        return novelMapper.list();
    }

    @Override
    public PageInfo<Novel> list1(Novel novel, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        List<Novel> list1 = novelMapper.list1(novel);
        PageInfo<Novel> pageInfo = new PageInfo<>(list1);
        return pageInfo;
    }

    @Override
    public Novel findbyid(Integer id) {
        return novelMapper.findbyid(id);
    }

    @Override
    public List<Novel> findOneNovel(NovelVo novelVo) {
        //添加历史记录
        HistorytagsVo historytagsVo = new HistorytagsVo();
        historytagsVo.setContent(novelVo.getKeyword());
        historytagsVo.setUserId(novelVo.getUserId());
        historytagsVo.setCreateTime(new Date());
        HistoryTags one = historyTagsMapper.findOne(historytagsVo);
        if (one!=null){
            //修改这条历史纪录的时间
            historyTagsMapper.updateTags(historytagsVo);
        }else{
            //添加一条历史记录
            HistoryTags historyTags = new HistoryTags();
            historyTags.setUserId(novelVo.getUserId());
            historyTags.setCreateTime(new Date());
            historyTags.setContent(novelVo.getKeyword());
            historyTagsMapper.saveHistory(historyTags);
        }
        //判断标签
        if (novelVo.getKeyword().equals("玄幻")){
            novelVo.setKeyword("1");
        }else if (novelVo.getKeyword().equals("都市")){
            novelVo.setKeyword("2");
        }else if(novelVo.getKeyword().equals("言情")){
            novelVo.setKeyword("3");
        }else if (novelVo.getKeyword().equals("历史")){
            novelVo.setKeyword("4");
        }else{
            novelVo.setKeyword(novelVo.getKeyword());
        }
        List<Novel> list = novelMapper.findOneNovel(novelVo);
        return list;
    }

    @Override
    public List<Map<String, Object>> findRankingNovel() {
        List<Map<String,Object>> list = new ArrayList<>();

        List<Score> scoreList = scoreMapper.findAllScore();

        for (int i = 0; i < scoreList.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            Score score = scoreList.get(i);
            Novel novel = novelMapper.findOne(score.getPid());

            map.put("pid",score.getPid());
            map.put("name",novel.getName());
            map.put("readCount",score.getAverage().toString());
            list.add(map);
        }
        return list;
    }

    @Override
    public Novel findOne(Integer pid) {
        return novelMapper.findOne(pid);
    }

    @Override
    public void add(Novel novel) {
        novelMapper.add(novel);
    }

    @Override
    public Novel findById(int i) {
        return novelMapper.findById(i);
    }

    @Override
    public List<Novelsimplify> find(Integer sort) {
        return novelMapper.find(sort);
    }

    @Override
    public User findUserById(Integer uid) {
        return novelMapper.findUserById(uid);
    }

    @Override
    public void update(Novel novel) {
        novelMapper.update(novel);
    }

    @Override
    public void delete(Integer id) {
        novelMapper.delete(id);
    }

    @Override
    public void updatetwo(Novel novel) {
        novelMapper.updatetwo(novel);
    }

}
